package com.wzq.interceptor;

import com.wzq.controller.ResumeController;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author wzq.Jolin
 * @company none
 * @create 2020-08-09 19:09
 */
@Component
public class LoginInterceptor   implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if (requestURI.equals("/") || requestURI.indexOf("/login") > 0) {
            return true;
        }
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute(ResumeController.SESSION_NAME);
        if (username != null && !"".equals(username.trim()) && ResumeController.USER_NAME.equals(username)) {
            // 已经登录
            return true;
        }
        //不符合条件的给出提示信息，并转发到登录页面
        request.setAttribute("msg", "您还没有登录，请先登录！");
        request.setAttribute("status", 0);
        request.getRequestDispatcher("/").forward(request, response);
        return false;
    }
}
